<?php
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

/*
pauli
特殊主体，必须按照这个格式来生成一个类
*/
class threadplugin_donkey_activity {

	var $name = '';
	var $buttontext = '';
	var $iconfile = '';
	var $cfg = array();

	//构造函数
	function threadplugin_donkey_activity() {
		global $_G;
		$this->name = lang('plugin/donkey_activity', 'name');
		$this->buttontext = lang('plugin/donkey_activity', 'buttontext');
		$this->iconfile = 'source/plugin/donkey_activity/static/icon_post.gif';
		$this->_load_config();
	}
	//加载配置文件，初始化
	function _load_config(){
		global $_LV;
		global $_G;
		require_once DISCUZ_ROOT.'./source/plugin/donkey_activity/donkey_activity.func.php';
		$this->cfg['basic'] = $_LV['basic'];
		$this->cfg['pay'] = $_LV['pay'];
		$this->cfg['express'] = $_LV['express'];
	}
	//发帖模板，调用 /source/plugin/donkey_activity/template/post.htm
	function newthread($fid) {
		global $_G;
		//TODO，加载数据库必要的数据
		$query = DB::query('SELECT * FROM '.DB::table('donkey_activity_property').' WHERE enable<4 ORDER BY showorder');
		$properties = array();
		$all_field='';
		while($group = DB::fetch($query)) {
			$group['showvalue'] = get_default_field($group['type'], $group['value'], $group['defval'], $group['propid'], '', $group['enable']);
			$group['showwidth'] = 'width_post_'.$group['showwidth'];
			//$group['message'] = $group['propid'].'_message';
			$properties[$group['propid']] = $group;
			if($group['enable']==1){
				$all_field .= ','.$group['propid'];
			}
		}
		//var_dump($properties);
		//exit(1);

		$rules=getUIRules();
		
		$merges=getUIMerges();
		
		include template('donkey_activity:post');
		return $return;
	}
	//发帖保存前处理业务逻辑
	function newthread_submit($fid) {
		global $_G;
		$query = DB::query('SELECT * FROM '.DB::table('donkey_activity_property').' WHERE enable<3 ORDER BY showorder');
		$properties = array();
		$pre='';
		while($group = DB::fetch($query)) {
			$pre='gp_'.$group['propid'];
			if($group['enable'] == 1 && (empty($_G[$pre]) && empty($_G[$pre.'_defval']) && empty($_G[$pre.'message']))){	//必须填写
				showmessage($group['propname'].'不能为空'.$pre.'：'.$_G[$pre].'：'.$_G[$pre.'_defval'].'：'.$_G[$pre.'message']);
			}
		}
	}
	//发帖保存后处理业务逻辑
	function newthread_submit_end($fid, $tid) {
		global $_G,$pid;
		$query = DB::query('SELECT * FROM '.DB::table('donkey_activity_property').' WHERE enable<3 ORDER BY showorder');
		$properties = array();
		$properties['tid']=$tid;
		$properties['uid']=$_G['uid'];
		$pre='';
		while($group = DB::fetch($query)) {
			$pre='gp_'.$group['propid'];
			if(empty($_G[$pre])){
				if(!empty($_G[$pre.'_defval'])){
					$_G[$pre]=$_G[$pre.'_defval'];
				} else if(!empty($_G[$pre.'message'])){
					$_G[$pre]=$_G[$pre.'message'];
				}
			}
			if(!empty($_G[$pre])){
				$properties[$group['propid']]=$_G[$pre];
			}
		}
		DB::insert('donkey_activity_thread', $properties);
	}
	//编辑帖子，打开页面
	function editpost($fid, $tid) {
		global $_G;
		
		$activity = DB::fetch_first('SELECT * FROM '.DB::table('donkey_activity_thread').' WHERE tid='.$tid);
		
		//TODO，加载数据库必要的数据
		$query = DB::query('SELECT * FROM '.DB::table('donkey_activity_property').' WHERE enable<3 ORDER BY showorder');
		$properties = array();
		$all_field='';
		while($group = DB::fetch($query)) {
			$group['showvalue'] = get_default_field($group['type'], $group['value'], $group['defval'], $group['propid'], '', $group['enable'], $activity[$group['propid']]);
			$group['showwidth'] = 'width_post_'.$group['showwidth'];
			//$group['message'] = $group['propid'].'_message';
			$properties[$group['propid']] = $group;
			if($group['enable']==1){
				$all_field .= ','.$group['propid'];
			}
		}
		//var_dump($properties);
		//exit(1);

		$rules=getUIRules();
		
		$merges=getUIMerges();
		
		include template('donkey_activity:post');
		return $return;
	}
	//编辑帖子，保存
	function editpost_submit($fid, $tid) {
		global $_G;
		$query = DB::query('SELECT * FROM '.DB::table('donkey_activity_property').' WHERE enable<3 ORDER BY showorder');
		$properties = array();
		$pre='';
		while($group = DB::fetch($query)) {
			$pre='gp_'.$group['propid'];
			if($group['enable'] == 1 && (empty($_G[$pre]) && empty($_G[$pre.'_defval']) && empty($_G[$pre.'message']))){	//必须填写
				showmessage($group['propname'].'不能为空'.$pre.'：'.$_G[$pre].'：'.$_G[$pre.'_defval'].'：'.$_G[$pre.'message']);
			}
		}
	}
	//编辑帖子，保存以后
	function editpost_submit_end($fid, $tid) {
		global $_G,$pid;
		$query = DB::query('SELECT * FROM '.DB::table('donkey_activity_property').' WHERE enable<3 ORDER BY showorder');
		$properties = array();
		//$properties['tid']=$tid;
		//$properties['uid']=$_G['uid'];	//修改帖子，就不改变活动发起人了
		$pre='';
		while($group = DB::fetch($query)) {
			$pre='gp_'.$group['propid'];
			if(empty($_G[$pre])){
				if(!empty($_G[$pre.'_defval'])){
					$_G[$pre]=$_G[$pre.'_defval'];
				} else if(!empty($_G[$pre.'message'])){
					$_G[$pre]=$_G[$pre.'message'];
				}
			}
			if(!empty($_G[$pre])){
				$properties[$group['propid']]=$_G[$pre];
			}
		}
		DB::update('donkey_activity_thread', $properties, 'tid='.$tid);

	}
	//编辑帖子，回复帖子
	function newreply_submit_end($fid, $tid) {
		global $_G;

	}
	//查看主题
	function viewthread($tid) {
		global $_G;
		
		$activity = DB::fetch_first('SELECT * FROM '.DB::table('donkey_activity_thread').' WHERE tid='.$tid);
		
		//TODO，加载数据库必要的数据
		$query = DB::query('SELECT * FROM '.DB::table('donkey_activity_property').' WHERE enable<3 ORDER BY showorder');
		$properties = array();
		$all_field='';
		while($group = DB::fetch($query)) {
			$group['showvalue'] = get_default_value($group['type'], $group['value'], $group['defval'], $activity[$group['propid']]);
			$group['showwidth'] = 'width_post_'.$group['showwidth'];
			$properties[$group['propid']] = $group;
			if($group['enable']==1){
				$all_field .= ','.$group['propid'];
			}
		}

		$activity['status'] = check_status($activity['status'], $activity['prop_3'], $tid);
		$properties['status']['showvalue'] = activity_status($activity['status'], 'img');

		if($activity['status'] == 0){

			$isactivitymaster = isactivitymaster($activity['uid']);

			if(empty($_G['uid'])) {
				if($activity['prop_22'] == 3){	//3=匿名用户可否报名
					$properties['status']['showvalue'] .= '<button value="" class="vmiddle big_btn" onclick="act_apply()">报名</button>';
				} else {
					$properties['status']['showvalue'] .= 
					'<button value="请登录后报名" class="vmiddle big_btn" onclick="showWindow(\'login\', \'member.php?mod=logging&amp;action=login&amp;referer=\');return false;" >请登录后报名</button>';
				}
			} else {
				$should_verify = $activity['prop_22'] == 2 && !isVerify($_G['uid']);
				//如果已经报名
				$query = DB::fetch_first("SELECT applyid FROM ".DB::table('donkey_activity_apply')." WHERE tid='$tid' AND uid='$_G[uid]'");
				if(!empty($query) && $query['applyid'] > 0){
					$applied = 1;
				}
				
				//if($activity['uid'] == $_G['uid']){	//活动发起者，TODO，是否可以报名？
				//} else 
				
				if(empty($query)){
					//实名验证
					if($should_verify){	//2=实名认证会员
						$properties['status']['showvalue'] .= '<a class="mid_btn ml80" href="home.php?mod=spacecp&ac=profile&op=verify">请填写实名信息后报名</a>';
					} else {
						$properties['status']['showvalue'] .= '<button value="" class="vmiddle big_btn" onclick="act_apply()">我要报名</button>';
						
						
						

						$activity['ufield'] = activity_ufield();
						if(empty($settings)) $settings = array('realname', 'mobile', 'qq');
						$settings['realname']['available']=true;
						$settings['mobile']['available']=true;
						$settings['qq']['available']=true;
						$settings['realname']['title']='真实姓名';
						$settings['mobile']['title']='手机号码';
						$settings['qq']['title']='QQ号';
						
						$query = DB::query("SELECT ".implode(',', $activity['ufield']['userfield'])." FROM ".DB::table('common_member_profile')." WHERE uid='$_G[uid]'");
						$ufielddata = array();
						$ufielddata['userfield'] = DB::fetch($query);
						
						$htmls=array();
						require_once libfile('function/profile');
						foreach($activity['ufield']['userfield'] as $fieldid) {
							$html = profile_setting($fieldid, $ufielddata['userfield'], false, true);
							if($html) {
								$settings[$fieldid] = $_G['cache']['profilesetting'][$fieldid];
								$htmls[$fieldid] = $html;
							}
						}

					}
				} else {
					$properties['status']['showvalue'] .= '<button value="" class="vmiddle big_btn" onclick="act_cancel()">取消报名</button>';
					//$properties['status']['showvalue'] .= '<button value="" class="vmiddle big_btn" onclick="print_apply(7, 3)">打印</button>';
				}

				if($activity['prop_23'] == 0 && !$should_verify){	//替人报名
					$properties['status']['showvalue'] .= '<button value="" class="vmiddle big_btn" onclick="act_apply4other()">替人报名</button>';
				}
			}
		}
		$merges=getUIMerges();

//用户分组
$all_group=all_group();
$all_role=activity_role(NULL,'list');

		//pauli,暂未通过报名验证的会员列表
$applylist = $applylistverified = $noverifiednum = $applynumbers = array();
$noverifiednum['ALL'] = $noverifiednum['GG'] = $noverifiednum['MM'] = $noverifiednum['--'] = $applynumbers['ALL'] = $applynumbers['GG'] = $applynumbers['MM'] = $applynumbers['--'] = 0;
$query = DB::query("SELECT aa.applyid, aa.username, aa.uid, aa.verified, aa.dateline, aa.message, aa.payment, aa.ufielddata, aa.roles, mp.gender, m.groupid FROM ".DB::table('donkey_activity_apply')." aa
	LEFT JOIN ".DB::table('common_member')." m USING(uid)
	LEFT JOIN ".DB::table('common_member_profile')." mp USING(uid)
	LEFT JOIN ".DB::table('common_member_field_forum')." mf USING(uid)
	WHERE aa.tid='$_G[tid]' ORDER BY aa.dateline DESC");

//pauli,参加报名的会员列表
require_once libfile('function/profile');
loadcache('profilesetting');

while($activityapplies = DB::fetch($query)) {
	$activityapplies['dateline'] = dgmdate($activityapplies['dateline'], 'u');
	$activityapplies['ufielddata'] = unserialize_fielddata($activityapplies['ufielddata']);
	$activityapplies['sex'] = get_gender($activityapplies['gender']);
	$activityapplies['verified_show'] = apply_status($activityapplies['verified']);
	$activityapplies['avatar'] = avatar($activityapplies['uid'], 'small');
	$activityapplies['message'] = preg_replace("/(".lang('forum/misc', 'contact').".*)/", '', $activityapplies['message']);

	if($activityapplies['verified'] == 1) {	//pauli,通过验证，数据库表donkey_activity_apply中verified字段标记为1
		$activityapplies['verified_show'] .= activity_role($activityapplies['roles'],'show');
		$applylist[] = $activityapplies;
		$noverifiednum['ALL']++;
		$noverifiednum[$activityapplies['sex']]++;
	} else {
		$applylistverified[] = $activityapplies;
		$applynumbers['ALL']++;
		$applynumbers[$activityapplies['sex']]++;
	}
}

		include template('donkey_activity:viewthread');
		return $return;
	}
}
?>